package com.web.main.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * @create 2020-11-21 17:26
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //配置docket和Swagger参数
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示swagger的环境
        Profiles of = Profiles.of("dev","test");
        //判断当前是否处于该环境
        //在配置文件配置为开发阶段。spring.profiles.active=dev
        boolean flag = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())  //
                .groupName("changshun")  //设置分组。可以查看分组下所有接口。
                .enable(flag)  //flag为true，启用swagger。
                .select()  //配置扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.web.main.controller"))
//                .paths(PathSelectors.ant("/**"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security());  //统一填写token,swagger使用
    }

    private List<ApiKey> security() {
        return newArrayList(new ApiKey("token", "token", "header"));
    }

    //配置文档信息
    private ApiInfo apiInfo() {
        //作者信息
        return new ApiInfo(
                "Swagger",  //标题
                "后端接口",  //描述
                "1.0",  //版本
                "urn:tos",  //组织链接链接
                new Contact("李长顺", "", ""),  //联系人信息
                "Apache 2.0",  //许可
                "http://www.apache.org/licenses/LICENSE-2.0",  //许可链接
                new ArrayList<VendorExtension>());  //扩展
    }
}
